package com.rose.controller;


import com.rose.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
public class DeptConsumerController {


    // 消费者，不应该有service层
    // RestTemplate ......供我们直接调用 注册到spring中
    @Autowired
    private RestTemplate restTemplate; // 提供多种远程访问http的方法 restful服务模板，spring提供的

    // String url, Class<T> responseType, Map<String, ?> uriVariables

     private static final String REST_URL_PREFIX = "http://SPRINGCLOUDPROVIDERDEPT8081";
    // private static final String REST_URL_PREFIX = "http://localhost:8001";
    // SPRINGCLOUDPROVIDERDEPT8081
    // 使用ribbon 之后就可以不用写他了；


    @RequestMapping("/consumer/dept/add")
    public int add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Integer.class);
    }


    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
    }




}
